﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Utilities;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class HomeController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: Home
        public ActionResult Index()
        {
            object uid= Session["uid"];
            if (uid == null)
            {
                return Redirect("/Back/Home/Login");
            }
            return View();
        }
        public ActionResult Welcom()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="admin">管理员对象</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public ActionResult LoginDeal(Admin admin, string code)
        {
            {
                //查找是否有用户
                Admin adminInfo = db.Admin.SingleOrDefault(P => P.username == admin.username);

                var codes = Convert.ToString(Session["img"]);
                if (adminInfo != null)
                {
                    //有这个用户比对密码是否正确
                    if (admin.password == adminInfo.password)
                    {
                        if (code == codes)
                        {
                            Session["username"] = admin.username;
                            Session["uid"] = adminInfo.id;

                            return Json(new { code = 0, msg = "登录成功" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "验证码错误" });
                        }

                    }
                    else
                    {
                        return Json(new { code = -1, msg = "密码错误" });

                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "用户不存在" });
                }
            }

        }
      
        public ActionResult GetRandCode(int? id)
        {

            //2.获取随机数
            string code = StringHelper.GetRandomNumAndLetters(4);
            //3.生成验证码
            byte[] img = ImageHelper.CreateImage(code, 100, 30);
            //4.存储验证码到session中
            Session["img"] = code;

            //5.返回验证码图片到登录页面
            // byte[] img = ImageHelper.CreateImage(StringHelper.GetRandomNumAndLetters(4), 100, 30);
            return File(img, "image/jpeg");
        }
        //UploadImg图片生成
        public ActionResult UploadImg(HttpPostedFileBase file)
        {
            //获取原来图片名称
            string imgName = file.FileName;

            //imgName = suijizifuchuan.GenerateCheckCode(10);

            //获取images的路径
            string path = Server.MapPath("~/images/");
            //保存图片到images
            file.SaveAs(path + imgName);
            //返还数据

            return Json(new { code = 0, msg = "上传成功", path = imgName });
        }
    }
}